#if !defined(PERL_H)
#define PERL_H
//888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
//
//	File		 :	PerlUtil.h
//	Contents	 :	Header file for functions emulating perl utility
//	Creation Date:	25/01/2000
//	Last Modified:	25/01/2000	lg
//	tabs width	 :	every 4 characters
//
//888888888888888888888888888888888888888888888888888888888888888888888888888888888888888


#include <vector>
#include <algorithm>
#include <string>
namespace perlutil
{

template<typename T, template< typename T> class CONT >
std::string
perl_join (const CONT<T>& c, const std::string& link)
{
	typedef typename CONT<T>::const_iterator citer;
	citer i = c.begin();
	citer end = c.end();
	std::string joined_str;
	if (i != end)
		joined_str += *i++;
	while (i != end)
	{
		joined_str += link;
		joined_str += *i++;
	}
	return joined_str;
}




enum eHANDLE_EMPTY_FIELDS{eCOUNT_EMPTY, eIGNORE_EMPTY};



template <typename CONTAINER>
void perl_split (const std::string&						str,
				const std::string&						delimiters,
				eHANDLE_EMPTY_FIELDS					empty_fields,
				CONTAINER&								ret,
				unsigned 								max_cnt = 1000)
{
	// array of strings to return
	ret.clear();

	std::string::size_type beg = 0;
	std::string::size_type end = std::string::npos;
	while (ret.size() + 1 < max_cnt &&
		   (end = str.find_first_of(delimiters, beg)) != std::string::npos)
	{
		if (end != beg || empty_fields == eCOUNT_EMPTY)
			ret.push_back(str.substr(beg, end - beg));
		beg = end + 1;
	}

	// handle last string
	ret.push_back(str.substr(beg));
}


// CONTAINER = eg. vector<pair<string::iterator, string::iterator> >&  	ret,
template <typename CONTAINER, typename ITER>
void perl_split (const ITER&								str_beg,
				const ITER&								str_end,
				const std::string&						delimiters,
				eHANDLE_EMPTY_FIELDS					empty_fields,
				CONTAINER&								ret,
				unsigned 								max_cnt = 1000)
{
	// array of strings to return
	ret.clear();

	ITER find_start	=	str_beg;
	ITER found		=	str_end;
	std::string::const_iterator delim_beg = delimiters.begin();
	std::string::const_iterator delim_end = delimiters.end();

	while (ret.size() + 1 < max_cnt &&
		   (found = std::find_first_of(find_start, str_end, delim_beg, delim_end)) != str_end)
	{
		if (empty_fields == eCOUNT_EMPTY ||
			find_start != found)
			ret.push_back(std::make_pair(find_start, found));
		find_start = found + 1;
	}

	// handle last string
	ret.push_back(std::make_pair(find_start, str_end));
}


}

#endif	//PERL_H
